package server.modules.generator.controller;

import java.util.*;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import server.modules.generator.entity.MeetNoticeEntity;
import server.modules.generator.entity.MeetingEntity;
import server.modules.generator.entity.PointEntity;
import server.modules.generator.service.MeetNoticeService;
import server.modules.generator.service.MeetingService;
import server.common.utils.PageUtils;
import server.common.utils.R;
import server.modules.generator.service.PointService;
import server.modules.generator.service.ProjectService;


/**
 * 会议
 *
 * @author
 * @email
 */
@RestController
@RequestMapping("generator/meeting")
public class MeetingController {
    @Autowired
    private MeetNoticeService meetNoticeService;
    @Autowired
    private MeetingService meetingService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private PointService pointService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:meeting:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = meetingService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:meeting:info")
    public R info(@PathVariable("id") Long id) {
        MeetingEntity meeting = meetingService.getById(id);

        return R.ok().put("meeting", meeting);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:meeting:save")
    public R save(@RequestBody MeetingEntity meeting) {
        meeting.setNotice("未通告");
        meeting.setFlag("预约中");
        meetingService.save(meeting);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:meeting:update")
    public R update(@RequestBody MeetingEntity meeting) {
        meetingService.updateById(meeting);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:meeting:delete")
    public R delete(@RequestBody Long[] ids) {
        meetingService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    @RequestMapping("/tonggao")
    public R tonggao(@RequestBody Long[] ids) {

        List<Long> idList = Arrays.asList(ids);


        for (Long id : idList) {

            MeetingEntity meetingEntity = meetingService.getById(id);
            meetingEntity.setNotice("已通知");
            meetingService.updateById(meetingEntity);


            // 创建通知记录
            Long proId = meetingEntity.getProjectId();
            List<PointEntity> pointEntities = pointService.lambdaQuery().eq(PointEntity::getProjectId, proId).list();

            List<Long> toUserS = new ArrayList<>();
            for (PointEntity pointEntity : pointEntities) {
                if (!toUserS.contains(pointEntity.getOwnerId())) {
                    toUserS.add(pointEntity.getOwnerId());
                }
                if (!toUserS.contains(pointEntity.getDiyapeidianOwnerId())) {
                    toUserS.add(pointEntity.getDiyapeidianOwnerId());
                }
                if (!toUserS.contains(pointEntity.getGuidaoOwnerId())) {
                    toUserS.add(pointEntity.getGuidaoOwnerId());
                }
                if (!toUserS.contains(pointEntity.getJianzhuOwnerId())) {
                    toUserS.add(pointEntity.getJianzhuOwnerId());
                }
                if (!toUserS.contains(pointEntity.getJiegouOwnerId())) {
                    toUserS.add(pointEntity.getJiegouOwnerId());
                }
                if (!toUserS.contains(pointEntity.getShenheOwnerId())) {
                    toUserS.add(pointEntity.getShenheOwnerId());
                }
                if (!toUserS.contains(pointEntity.getTongfengOwnerId())) {
                    toUserS.add(pointEntity.getTongfengOwnerId());
                }

            }

            for (Long toUser : toUserS) {
                if (null != toUser) {
                    MeetNoticeEntity meetNoticeEntity = new MeetNoticeEntity();
                    meetNoticeEntity.setMeetId(id);
                    meetNoticeEntity.setToUserId(toUser);
                    meetNoticeEntity.setType("会议通知");
                    meetNoticeEntity.setCreateTime(new Date());
                    meetNoticeEntity.setFlag("未签到");
                    meetNoticeEntity.setContext("收到会议通知，请技术处理！会议主题：" + meetingEntity.getTitle() + "，会议概要：" + meetingEntity.getContext());
                    meetNoticeService.save(meetNoticeEntity);
                }
            }

        }
        return R.ok();
    }
    @RequestMapping("/start")
    public R start(@RequestBody Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        for (Long id : idList) {
            MeetingEntity meetingEntity = meetingService.getById(id);
            meetingEntity.setFlag("进行中");
            meetingService.updateById(meetingEntity);
        }
        return R.ok();
    }
    @RequestMapping("/end")
    public R end(@RequestBody Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        for (Long id : idList) {
            MeetingEntity meetingEntity = meetingService.getById(id);
            meetingEntity.setFlag("会议结束");
            meetingService.updateById(meetingEntity);
        }
        return R.ok();
    }

}
